package org.flown.shop.controller;


import jakarta.servlet.http.HttpServletResponse;
import org.flown.shop.dto.GoodsDTO;
import org.flown.shop.entity.Goods;
import org.flown.shop.entity.error.ReadError;
import org.flown.shop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Base64;


@RequestMapping("/goods")
@Controller
public class GoodsController
{
    @Autowired
    private GoodsService service_goods;


    /**
     * id
     * name
     * price
     * sellerID
     * storeID
     */
    @ResponseBody
    @RequestMapping(value = {"/getGoodsByID", "/{id}"})
    public GoodsDTO getGoodsByID(@PathVariable @RequestParam("id") int id) {return this.service_goods.getGoodsDTOByID(id);}




    @ResponseBody
    @RequestMapping(value = {"/getGoodsDetailByID", "/{id}/detail"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String getGoodsDetailByID(@PathVariable @RequestParam("id") int id)
    {
        GoodsDTO goods_dto = this.getGoodsByID(id);
        if (goods_dto == null)
            return "商品不存在";
        return goods_dto.getDetail();
    }


    @ResponseBody
    @RequestMapping(value = {"/getGoodsCoverByID", "/{goods_id}/cover"}, produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getGoodsCoverByID(@PathVariable @RequestParam("id") int goods_id)
            throws IOException
    {
        GoodsDTO goods_dto = this.getGoodsByID(goods_id);
        if (goods_dto == null)
            return null;
        return goods_dto.getCoverPicture();
    }


    /**
     * 返回一张图片的字节集
     * */
    @RequestMapping(value = {"/getGoodsPictureByID", "/{goods_id}/picture/{picture_index}"})
    public void getGoodsPictureByIDAndIndex(HttpServletResponse response,
                                            @PathVariable @RequestParam("id") int goods_id,
                                            @PathVariable @RequestParam("index") int picture_index)
            throws IOException
    {
        GoodsDTO goods_dto = this.getGoodsByID(goods_id);
        if (goods_dto == null)
        {
            PrintWriter out = response.getWriter();
            out.println("商品不存在");
            out.close();
            return;
        }

        try {
            byte[] bytes = goods_dto.getPicture(picture_index);
            String format = goods_dto.getPictureFormat(picture_index);

            switch (format) {
                case "jpg", "jpeg" -> response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                case "png" -> response.setContentType(MediaType.IMAGE_PNG_VALUE);
                case "gif" -> response.setContentType(MediaType.IMAGE_GIF_VALUE);
                default -> throw new ReadError("unknown picture format");
            }

            OutputStream out = response.getOutputStream();
            out.write(bytes);
            out.close();
        } catch (ReadError e) {
            e.printStackTrace();
            PrintWriter out = response.getWriter();
            out.println("图片不存在");
            out.close();
        }
    }


    /**
     * @return 返回base64编码后的图片
     * */
    @ResponseBody
    @RequestMapping(value = {"/getGoodsPicturesByID", "/{id}/pictures"})
    public String[] getGoodsPicturesByID(@PathVariable @RequestParam("id") int goods_id)
            throws ReadError, IOException
    {
        GoodsDTO goods_dto = this.getGoodsByID(goods_id);
        if (goods_dto == null)
            return null;
        byte[][] pictures = goods_dto.getPictures();
        String[] base64_pictures = new String[pictures.length];
        for (int i = 0; i < pictures.length; i++)
            base64_pictures[i] = Base64.getEncoder().encodeToString(pictures[i]);
        return base64_pictures;
    }



    /**
     * id 必要
     * name
     * price
     * */
    public String updateGoodsByID(Goods goods)
    {
        if (this.service_goods.updateGoodsByID(goods))
            return "更新成功";
        return "更新失败";
    }


    /**
     * sellerID
     * storeID
     * name
     * price
     * */
    public void insertGoods(Goods goods) {this.service_goods.insertGoods(goods);}

    public void deleteGoodsByID(int id) {this.service_goods.deleteGoodsByID(id);}
}














